package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.impl.zcl.Bitmap;

/**
 * Device capability information. Used in MACAssociateIndication and MACAssociateRequest.
 *
 * @author AKovylin
 */
public class Capability extends Bitmap {
    public Capability(boolean panCoordCapable, boolean ffd, boolean alternatingPower, boolean rxOnWhenidle,
                      boolean securityCapable, boolean allocateAddres) {
        this(TypeCapability.TYPE, new byte[]{0});
        super.setBit(0, panCoordCapable);
        super.setBit(1, ffd);
        super.setBit(2, alternatingPower);
        super.setBit(3, rxOnWhenidle);
        super.setBit(6, securityCapable);
        super.setBit(7, allocateAddres);
    }

    Capability(TypeCapability type, byte[] value) {
        super(type, value);
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        if (value.length != 1) {
            throw new IllegalArgumentException("invalid value");
        }
        if (getBit(4) || getBit(5)) {
            throw new IllegalArgumentException("reserved bit(s) is(are) set");
        }
    }

    public boolean isPanCoordCapable() {
        return getBit(0);
    }

    public boolean isFFD() {
        return getBit(1);
    }

    public boolean isAlternatingPower() {
        return getBit(2);
    }

    public boolean isRxOnWhenidle() {
        return getBit(3);
    }

    public boolean isSecurityCapable() {
        return getBit(6);
    }

    public boolean isAllocateAddress() {
        return getBit(7);
    }
}
